package com.xgr.wonderful.activity.base;

import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.xgr.wonderful.MyApplication;
import com.xgr.wonderful.constant.Constant;
import com.xgr.wonderful.utils.Sputil;

import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

/**
 * fragmentactivity 继承自activity，用来解决android3.0 
 * 之前没有fragment的api，所以在使用的时候需要导入support包，同
 * 时继承fragmentActivity，这样在activity中就能嵌入fragment
 * 来实现你想要的布局效果。
 *  最基础的activity
 *  派生了两个类：BaseHomeActivity以及BasePageActivity
 *  它们分别作为更高一级的基础类派生了其他面向实际功能的activity
 * @author lsj
 *
 */
public class BaseActivity extends FragmentActivity implements OnSharedPreferenceChangeListener{
	
	protected static String TAG ;
	
	// 每个activity包含application的引用
	protected MyApplication mMyApplication;
	// 每个activity有自己的sharedpreference
	protected Sputil sputil;
	protected Resources mResources;
	protected Context mContext;
	
	protected ProgressDialog mProgressDialog ;
	
	protected NotificationManager notificationManager ;
	
	@Override
	protected void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		super.onCreate(bundle);
		TAG = this.getClass().getSimpleName();
		initConfigure();
		//如果不调用此方法，将会导致按照"几天不活跃"条件来推送失效。
		PushAgent.getInstance(mContext).onAppStart();
	}


	private void initConfigure() {
		mContext = this;
		if(null == mMyApplication){
			mMyApplication = MyApplication.getInstance();
		}
		// 向application注册自己
		mMyApplication.addActivity(this);
		if(null == sputil){
			sputil = new Sputil(this, Constant.BASIC_PRE_NAME);
		}
		// 监听sharedpreference被修改事件
		sputil.getInstance().registerOnSharedPreferenceChangeListener(this);
		mResources = getResources();
		notificationManager = (NotificationManager)getSystemService(
				Context.NOTIFICATION_SERVICE);
	}


	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// TODO Auto-generated method stub
		//可用于监听设置参数，然后作出响应
	}

	/**
	 * Activity跳转
	 * @param context
	 * @param targetActivity
	 * @param bundle
	 */
	public void redictToActivity(Context context,Class<?> targetActivity,Bundle bundle){
		Intent intent = new Intent(context, targetActivity);
		if(null != bundle){
			intent.putExtras(bundle);
		}
		startActivity(intent);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//umeng统计 分析
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//umeng统计 分析
		MobclickAgent.onPause(this);
	}
	
	/**
	 * true :可以中止
	 * false:不可中止
	 * @param msg
	 * @param cancelable
	 */
	protected void  showProgressDialog(String msg , boolean cancelable) {
		if (null== mProgressDialog){
			mProgressDialog = new ProgressDialog(this );
		}
		mProgressDialog.setCancelable(cancelable);
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
	}
	
	protected void  cancelProgressDialog() {
		if (null != mProgressDialog){
			mProgressDialog.cancel();
		}
		mProgressDialog =null;
	}

}
